package com.shqcjd.siemensglass.http;

import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.widget.Toast;


import com.shqcjd.siemensglass.MyApp;
import com.shqcjd.siemensglass.utils.LoadingUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import es.dmoral.toasty.Toasty;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

import static io.reactivex.disposables.Disposables.disposed;


public class Response<T> implements Observer<T> {
    private Context mContext;
    private boolean mNeedDialog = false;
    private onCancelRequestListener cancelRequestListener;

    public Response(Context context) {
        this.mContext = context;
    }

    public Response(Context context, boolean needDialog) {
        this.mContext = context;
        this.mNeedDialog = needDialog;
    }

    public Response(Context context, boolean needDialog, onCancelRequestListener cancelRequestListener) {
        this.mContext = context;
        this.mNeedDialog = needDialog;
        this.cancelRequestListener = cancelRequestListener;
    }

    @Override
    public void onSubscribe(Disposable d) {
        if (mNeedDialog) {
            LoadingUtils.show(mContext);
            LoadingUtils.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                    if (i == KeyEvent.KEYCODE_BACK) {
                        disposed();
                        LoadingUtils.dismiss();
                        if (cancelRequestListener != null) {
                            cancelRequestListener.onCancelRequest();
                        }
                    }
                    return false;
                }
            });
        }
    }

    @Override
    public void onNext(T t) {
        // TODO: 2018/5/8 需要判断账号是否被冻结打开
//        boolean flag = false;
//        if(t instanceof BaseResult){
//            if(((BaseResult) t).code == 600){
//                flag =true;
//            }
//        }
//        if(t instanceof Base2Result){
//            if("600".equals(((Base2Result)t).response)){
//                flag =true;
//            }
//        }
//        if(flag){
//            ToastCommom.createToastConfig().ToastShow(MyApp.applicationContext, "很抱歉！您的帐号已经被冻结，无法登录使用");
//            Intent intent = new Intent(mContext,LoginActivity.class);
//            if (mContext!=null && ( !(mContext instanceof LoginActivity))){
//                mContext.startActivity(intent);
//                ((Activity)mContext).finish();
//            }
//            throw new RuntimeException(new Throwable("login fail"));
//        }
    }

    /**
     * 除非非要获取网络错误信息，否则一般不需要重写此方法；
     * 例如：网络400，404，断网，超时，等等…………
     */
    @Override
    public void onError(Throwable e) {
        LoadingUtils.dismiss();
        try {
            if (e instanceof ConnectException || e instanceof UnknownHostException) {
                Toasty.info(MyApp.applicationContext,"没有网络", Toast.LENGTH_SHORT).show();
            } else if (e instanceof SocketTimeoutException) {
                Toasty.info(MyApp.applicationContext,"连接超时", Toast.LENGTH_SHORT).show();
            } else if (((HttpException) e).code() == 401) {
            } else if (e instanceof HttpException) {
                Toasty.info(MyApp.applicationContext,"网络异常", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception ignored) {

        }

        if (e == null || mContext == null)
            return;
    }

    @Override
    public void onComplete() {
        if (mNeedDialog) {
            LoadingUtils.dismiss();
        }
        mContext = null;
    }

    public interface onCancelRequestListener {
        void onCancelRequest();
    }
}